package xin.homeant.upms.service.impl;

import xin.homeant.upms.model.Permission;
import xin.homeant.upms.dao.mapper.PermissionMapper;
import xin.homeant.upms.service.IPermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author homeant
 * @since 2017-05-01
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

	@Override
	public List<Permission> getUserPermissions(Integer id) {
		return getChildPermissionsByUidAndPid(id,null);
	}
	
	/**
	 * <p>getChildPermissionsByUidAndPid</p>
	 * <p>
	 * 递归获取用户权限数据
	 * </p>
	 * @param uid
	 * @param pid
	 * @return
	 * @author homeant junchen1314@foxmail.com
	 * @Data 2017年5月12日 上午12:12:27
	 */
	private List<Permission> getChildPermissionsByUidAndPid(Integer uid,Integer pid){
		List<Permission> permissions = this.baseMapper.getChildPermissionsByUidAndPid(uid,pid);
		for (Permission permission : permissions) {
			int rows = getChildCountByUidAndPid(uid,permission.getParentId());
			if(rows > 0){
				permission.setChilds(getChildPermissionsByUidAndPid(uid,permission.getId()));
			}
		}
		return permissions;
	}
	
	private int getChildCountByUidAndPid(Integer uid,Integer pid){
		return this.baseMapper.getChildCountByUidAndPid(uid,pid);
	}

	@Override
	public Set<String> getUserPermissions(Set<Integer> permissionIds) {
		return baseMapper.getUserPermissions(permissionIds);
	}
}
